package com.wbcs.system.dataimport.queue;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.fileupload.FileItem;

import com.wbcs.system.dataimport.DataImportItem;

public class UploadFilesQueue
{
    private static UploadFilesQueue instance=new UploadFilesQueue();

    private List<Map<List<DataImportItem>,Map<File,FileItem>>> queueInstance;

    private UploadFilesQueue()
    {
        queueInstance=new ArrayList<Map<List<DataImportItem>,Map<File,FileItem>>>();
    }

    public static UploadFilesQueue getInstance()
    {
        return instance;
    }

    public void addUploadFile(Map<List<DataImportItem>,Map<File,FileItem>> uploadFile)
    {
        synchronized(queueInstance)
        {
            if(uploadFile!=null&&uploadFile.size()>0)
            {
                queueInstance.add(uploadFile);
                queueInstance.notifyAll();
            }
        }
    }

    public Map<List<DataImportItem>,Map<File,FileItem>> getUploadFile()
    {
        synchronized(queueInstance)
        {
            while(queueInstance.size()==0)
            {
                try
                {
                    queueInstance.wait();
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            return queueInstance.remove(0);
        }
    }

    public List<Map<List<DataImportItem>,Map<File,FileItem>>> getLstAllUploadFiles()
    {
        synchronized(queueInstance)
        {
            while(queueInstance.size()==0)
            {
                try
                {
                    queueInstance.wait();
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            List<Map<List<DataImportItem>,Map<File,FileItem>>> lstResults=new ArrayList<Map<List<DataImportItem>,Map<File,FileItem>>>();
            lstResults.addAll(queueInstance);
            queueInstance.clear();
            return lstResults;
        }
    }

    public void notifyAllThread()
    {
        synchronized(queueInstance)
        {
            queueInstance.notifyAll();
        }
    }
}
